var main = angular.module('main');

main.controller('add_apiCtrl', ['$scope', 'http', '$stateParams', '$state', function($scope, http, $stateParams, $state) {
    $scope.project_id = $stateParams.project_id;
    $scope.groups = [];


    $scope.url = "";
    $scope.desc = "";
    $scope.json = "";
    $scope.newParams = [];
    $scope.newReturns = [];
    $scope.newStatus = [];
    $scope.add = {};
    $scope.add.name = '';
    $scope.add.type = 'string';
    $scope.add.restriction = '';
    $scope.add.required = 'true';
    $scope.add.returnName = '';
    $scope.add.returnType = 'string';
    $scope.add.returnRemark = "";
    $scope.add.statusCode = '';
    $scope.add.desc = '';
    $scope.add.remark = "";

    http.post('/api/group/list', { project_id: $scope.project_id }).then(function(data) {
        $scope.groups = data.data;
        $scope.groupSelected = $scope.groups[0].id;
    });

    http.get('/api/method/list').then(function(data) {
        $scope.methods = data.data;
    });
    $scope.methodSelected = [];
    $scope.toggleSelection = function(method) {
        var index = $scope.methodSelected.indexOf(method.value);
        if (index > -1)
            $scope.methodSelected.splice(index, 1);
        else
            $scope.methodSelected.push(method.value);
    };

    $scope.types = [];
    http.get('/api/type/list').then(function(data) {
        $scope.types = data.data;
        $scope.add.type = $scope.types[0];
        $scope.add.returnType = $scope.types[0];
    });


    $scope.deleteParam = function(name) {
        for (var i = 0; i < $scope.newParams.length; i++) {
            if ($scope.newParams[i].name == name) {
                $scope.newParams.splice(i, 1);
                break;
            }
        }
    };
    $scope.addParam = function() {
        var newParam = { 'name': $scope.add.name, 'type': $scope.add.type.value, 'stype': $scope.add.type, 'restrict': $scope.add.restriction, 'remark': $scope.add.remark };
        if ($scope.add.required == 'true') {
            newParam.necessary = true;
            newParam.required = "是";
        } else {
            newParam.necessary = false;
            newParam.required = "否";
        }
        $scope.newParams.push(newParam);
        $scope.add.name = '';
        $scope.add.restriction = '';
        $scope.add.remark = '';
    };
    $scope.deleteReturn = function(name) {
        for (var i = 0; i < $scope.newReturns.length; i++) {
            if ($scope.newReturns[i].name == name) {
                $scope.newReturns.splice(i, 1);
                break;
            }
        }
    };
    $scope.addReturn = function() {
        var newReturn = { 'name': $scope.add.returnName, 'type': $scope.add.returnType.value, 'stype': $scope.add.returnType, 'remark': $scope.add.returnRemark };
        $scope.newReturns.push(newReturn);
        $scope.add.returnName = '';
        $scope.add.returnRemark = '';
    };
    $scope.deleteStatus = function(code) {
        for (var i = 0; i < $scope.newStatus.length; i++) {
            if ($scope.newStatus[i].statusCode == code) {
                $scope.newStatus.splice(i, 1);
                break;
            }
        }
    };
    $scope.addStatus = function() {
        var newS = { 'code': $scope.add.statusCode, 'remark': $scope.add.desc };
        $scope.newStatus.push(newS);
        $scope.add.statusCode = '';
        $scope.add.desc = '';
    };

    $scope.submit = function() {
        if ($scope.url === "")
            UIkit.modal.alert('url不能为空');
        if ($scope.methodSelected.length===0)
            UIkit.modal.alert('访问方法不能为空');
        else {
            var postParam = {};
            postParam.url = $scope.url;
            postParam.method = JSON.stringify($scope.methodSelected);
            postParam.group_id = $scope.groupSelected;
            postParam.description = $scope.desc;
            postParam.output_example = $scope.json;
            postParam.request = JSON.stringify($scope.newParams);
            postParam.request_remark = "";
            postParam.output = JSON.stringify($scope.newReturns);
            postParam.output_remark = "";
            postParam.status = JSON.stringify($scope.newStatus);
            http.post('/api/add', postParam).then(function(data) {
                if (data.code === 0) {
                    UIkit.modal.alert("API添加成功！");
                    $state.go('project.apis', { project_id: $scope.project_id });
                }
            });
        }
    };
}]);
